package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.RechargeQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.Recharge;
import cn.org.eshow.model.User;
import cn.org.eshow.service.AccessTokenManager;
import cn.org.eshow.service.RechargeManager;
import cn.org.eshow.util.OrderUtil;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 充值API
 */
@AllowedMethods({"search","save", "pay"})
public class RechargeAction extends ApiBaseAction<Recharge> {

    @Autowired
    private RechargeManager rechargeManager;
    @Autowired
    private AccessTokenManager accessTokenManager;

    private List<Recharge> recharges = new ArrayList<Recharge>();
    private Recharge recharge = new Recharge();
    private RechargeQuery query = new RechargeQuery();

    /**
     * h5搜索查询
     *
     * @return
     */
    public void search() {
        User old = accessTokenManager.isValid(accessToken);
        if (old == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        query.setUserId(old.getId());
        Page<Recharge> page = rechargeManager.search(query);
        recharges = page.getDataList();
        RenderUtil.page("获取成功","recharges",  page, recharges);
    }

    /**
     * 添加充值
     */
    public void save() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        if(recharge.getPrice()==null || recharge.getPrice()<=0){
            RenderUtil.errorParame("缺少金额参数");
            return;
        }
        recharge.setNo(OrderUtil.getOrderNo());
        recharge.setState(0);
        recharge.setUser(user);
        recharge = rechargeManager.save(recharge);
        RenderUtil.success(recharge.getId().toString());
    }

    /**
     * 支付
     */
    public void pay() {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        if (id == null) {
            RenderUtil.errorParame("非法参数");
            return;
        }
        Recharge old = rechargeManager.get(id);
        if (old == null) {
            RenderUtil.errorParame("非法参数");
            return;
        }
        if (StringUtils.isEmpty(recharge.getChannel())) {
            RenderUtil.errorParame("请选择支付方式");
            return;
        }

        old.setUpdateTime(new Date());
        old.setChannel(recharge.getChannel());
        old = rechargeManager.save(old);

        RenderUtil.success(old.getId().toString());
        return;
    }

    public List<Recharge> getRechargees() {
        return recharges;
    }

    public void setRechargees(List<Recharge> recharges) {
        this.recharges = recharges;
    }

    public Recharge getRecharge() {
        return recharge;
    }

    public void setRecharge(Recharge recharge) {
        this.recharge = recharge;
    }

    public RechargeQuery getQuery() {
        return query;
    }

    public void setQuery(RechargeQuery query) {
        this.query = query;
    }

    public List<Recharge> getRecharges() {
        return recharges;
    }

    public void setRecharges(List<Recharge> recharges) {
        this.recharges = recharges;
    }

}
